package com.ftg.mall.base.util.tools;

import org.springframework.util.Assert;

import java.io.*;
import java.util.Objects;
import java.util.UUID;

/**
 * @author kangjx
 * @createTime 2022/11/21
 */
public class MyFileUtil {


    /**
     * 上传 :
     *    就是用户 在前端选择一个文件路径 ，把这个文件路径 传递后台的方法
     *    copy 后台完成读取以及写入
     *
     * 文件复制方法
     * @param fileName
     */
    public static void copy(String fileName){
        File file = Objects.nonNull(fileName) ? new File(fileName) : null;
        Assert.notNull(file,"请输入正确的文件地址!!!");
        String fileNameType = fileName.substring(fileName.lastIndexOf("."));
        String fileInDirectory =file.getAbsolutePath().substring(0,file.getAbsolutePath().lastIndexOf("/") + 1);
        String newFileName = UUID.randomUUID().toString() + fileNameType;

        File newFile = new File(fileInDirectory+newFileName);

        try(
                FileInputStream io = new FileInputStream(file);
                FileOutputStream out = new FileOutputStream(newFile);
        ){
            int num = 0;
            while( (num = io.read()) != -1 ){
                out.write(num);
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    /**
     *
     * @param fileName  前端给定的地址
     * @param target    后台生成的本机服务器地址 /aa/bb/
     */
    public static void copy(String fileName,String target){
        File file = Objects.nonNull(fileName) ? new File(fileName) : null;
        Assert.notNull(file,"请输入正确的文件地址!!!");
        String fileNameType = fileName.substring(fileName.lastIndexOf("."));

        String newFileName = UUID.randomUUID().toString() + fileNameType;

        File newFile = new File(target + newFileName);

        try(
                FileInputStream io = new FileInputStream(file);
                FileOutputStream out = new FileOutputStream(newFile);
        ){
            int num = 0;
            while( (num = io.read()) != -1 ){
                out.write(num);
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    /**
     *
     * @param hz  后缀
     * @param io  前端给定的流
     * @param target    后台生成的本机服务器地址 /aa/bb/
     */
    public static void copy(String hz,InputStream io,String target){
        String newFileName = UUID.randomUUID().toString() + hz;

        File newFile = new File(target + newFileName);

        try(
            FileOutputStream out = new FileOutputStream(newFile);
        ){
            int num = 0;
            while( (num = io.read()) != -1 ){
                out.write(num);
            }
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            try {
                io.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 带缓冲区的字符流，纯文本类处理较好
     * @param fileName  前台文件地址
     * @param target    后台存储地址  /aa/bb
     */
    public static void bufferCopy(String fileName,String target){

        Assert.notNull(fileName,"请输入正确的文件地址!!!");
        String fileNameType = fileName.substring(fileName.lastIndexOf("."));

        String newFileName = UUID.randomUUID().toString() + fileNameType;

        File newFile = new File(target + newFileName);

        try(
                BufferedReader reader1 = new BufferedReader(new FileReader(fileName));
                BufferedWriter writer = new BufferedWriter(new FileWriter(newFile));
        ) {
            while(reader1.ready()){
                writer.write(reader1.readLine() +"\n");
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
